package com.dwjm.interceptor;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dwjm.entity.SysUsers;
import com.dwjm.entity.User;
import com.dwjm.util.EncryptUtil;

/**
 * 登录过滤器
 * 
 * @author luans
 * @date 2016年12月19日
 *
 */
public class LoginFilter implements Filter {

 
	ServletContext application;
	private List<String> creatiaPCPath;// PC端需要登录的页面集合
	private List<String> creatiaHPath;// 后台需要登录的页面集合

	// 初始化
	public void init(FilterConfig config) throws ServletException {
		application = config.getServletContext();
		creatiaPCPath = new ArrayList<String>();
		creatiaPCPath.add("/newsctr/forum.htm");// 首页
		creatiaPCPath.add("/basicctr/pcbbsbasic.htm");// 论坛概况
		creatiaPCPath.add("/organize/pcolist.htm");// 机构设置
		creatiaPCPath.add("/newsctr/pcnewslist.htm");// 文案管理
		creatiaPCPath.add("/bbseplctr/pcbbslist.htm");// 论坛学人
		creatiaPCPath.add("/basicctr/pcbbsbasic.htm");// 联系我们
		creatiaPCPath.add("/colctr/col.htm");// 收藏网站操作
		

		creatiaHPath = new ArrayList<String>();
		creatiaHPath.add("/login/beforeLogin.do");//后台登录
		creatiaHPath.add("/login/index.htm");// 后台主页
		creatiaHPath.add("/login/firstpage.htm");// 后台首页
		creatiaHPath.add("/basicctr/bbsbasic.htm");// 论坛概括
		creatiaHPath.add("/organize/olist.htm");// 机构设置
		creatiaHPath.add("/newsctr/newslist.htm");// 文案管理
		creatiaHPath.add("/bbseplctr/bbslist.htm");// 论坛学人
		creatiaHPath.add("/basicctr/bbscontact.htm");// 联系我们
		creatiaHPath.add("/banctr/banlist.htm");// Banner管理

	

	}

	public void destroy() {
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		HttpSession session = request.getSession();
		Cookie cookies[] = request.getCookies();
		String cookieValue = "";

		// 记录上次请求地址(如果是地址栏直接访问，则referer为null)，防止用户篡改访问地址，直接访问action
		String path = request.getServletPath();// 访问地址
		//System.out.println("path:"+path);
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if ("back_user_cookie".equals(cookies[i].getName())) {
					cookieValue = URLDecoder.decode(cookies[i].getValue(),"utf-8");
				//	System.out.println("后台cookie值："+cookieValue);
				}else if ("pc_user_cookie".equals(cookies[i].getName())) {
					cookieValue = URLDecoder.decode(cookies[i].getValue(),"utf-8");
				} 
			}
		}
	/*	if ((cookieValue == null || "".equals(cookieValue)) && creatiaPCPath.contains(path)) {
			System.out.println("跳转到前台登录页面");
			session.setAttribute("beforePath", path);
			// 跳转到PC端登录页面
			response.sendRedirect(request.getContextPath() + "/login/gotoIndexPC.do");
		} else*/
			
			if ((cookieValue == null ||  "".equals(cookieValue)) && creatiaHPath.contains(path)) {
			//System.out.println("跳转到后台登录页面");
			session.setAttribute("beforePath", path);
			// 跳转到后台登录页面
			response.sendRedirect(request.getContextPath() + "/login/beforelogin.htm");
		} else {
			if (cookieValue != null && !"".equals(cookieValue)) {
				try {
					cookieValue = EncryptUtil.base64Decoder(cookieValue);
					String[] array = cookieValue.split(";");
					if (creatiaHPath.contains(path) && Integer.parseInt(array[3]) != 1) {
						// 跳转到后台登录页面
						response.sendRedirect(request.getContextPath()+ "/login/beforelogin.htm");
			/*		} else if (creatiaPCPath.contains(path) && Integer.parseInt(array[3]) != 2) {
						// 跳转到PC端登录页面
						response.sendRedirect(request.getContextPath()+ "/login/loginpc.htm");*/
					}else if(creatiaHPath.contains(path) && Integer.parseInt(array[3]) == 1){
						
						SysUsers sysuser = new SysUsers();
						sysuser.setSysuserId(Integer.parseInt(array[0]));
						sysuser.setSysuserName(array[1]);
						sysuser.setSysuserPwd(array[2]);
						session.setAttribute("back_user", sysuser);
					}else if(creatiaPCPath.contains(path) && Integer.parseInt(array[3]) == 2){
						
						User user=new User();
						user.setUid(Integer.parseInt(array[0]));
						user.setEmail(array[1]);
						user.setPwd(array[2]);
						user.setPccolid(Integer.parseInt(array[3]));
						session.setAttribute("pc_user", user);
						session.setAttribute("pc_userid", Integer.parseInt(array[0]));
					}
					 
				} catch (Exception e) {
					System.out.println("过滤器问题！");
				}
			}

			filterChain.doFilter(request, response);
		}
		 
	}

	 

}
